package com.sj;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class Test {
    //读取某个标签下的子标签
    static void  readXml() throws DocumentException
    {
        SAXReader reader = new SAXReader();
        Document document = reader.read(new File("src/main/resources/test.xml"));
        //取得根结点
        Element root = document.getRootElement();
        //取得根结点下面的所有student结点
        Element e1 = (Element) root.elements("student").get(1);
        //取得student结点下面的name结点
        String name = e1.element("name").getText();
        //取得name结点的属性结点
        String minzu = e1.element("name").attributeValue("minzu");
        System.out.println(name);

        System.out.println(minzu);
    }


    //修改某个标签
    public void testUpdate2() throws DocumentException, IOException {
        SAXReader reader = new SAXReader();
        Document document = reader.read(new File("src/main/resources/test.xml"));
        List studentlist = document.getRootElement().elements("student");
        Element student2 = (Element) studentlist.get(1);
        //取得所有属性
        List attributes = student2.elements();
        Element e = (Element) attributes.get(0);
        //更新其中的文本内容
        e.setText("4444");
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("utf-8");
        XMLWriter writer = new XMLWriter(
                new FileOutputStream( "src/main/resources/test.xml" ), format );
        writer.write( document);
        writer.close();
    }

    //指定位置添加节点
    @org.junit.Test
    public void addIndex()throws Exception{
        SAXReader reader = new SAXReader();
        Document document = reader.read(new File("src/main/resources/test.xml"));

        //得到第二个student标签
        Element student = (Element)
                document.getRootElement().elements("student").get(1);

        List list = student.elements();  //得到所有子结点[name,id,age]

        //创建新的结点
        Element addressnode = DocumentHelper.createElement("address");
        addressnode.setText("泉州");
        //插入新的结点,list变成了[name,address,id,age]，原来1后面的元素往后移动了一个位置
        list.add(1,addressnode);

        //重新写入
        //如果我们的xml编码格式为gb2312的话，那么字符集应该设置成format.setEncoding("gb2312");
        //也就是xml的字符集和匹配器设置的字符集得一致
        OutputFormat format = OutputFormat.createPrettyPrint();
        //这里指定的格式要和xml文档的编码格式一致，否则会出现乱码
        format.setEncoding("utf-8");

        //FileOutputStream的编码也是按照format设置的字符集进行编码
        XMLWriter writer = new XMLWriter(
                new FileOutputStream( "src/main/resources/test.xml" ), format );

        //写入
        writer.write( document);
        //关闭
        writer.close();

    }


    //往指定位置插入新的结点,dom4j中没有insertbefore方法，要在指定位置插入，必须取得父节点
    //的elements集合，然后修改elements集合，实现指定位置插入
    static void  insertToXml() throws DocumentException, IOException
    {
        SAXReader reader = new SAXReader();
        Document document = reader.read(new File("src/student.xml"));

        //得到第二个student标签
        Element student = (Element)
                document.getRootElement().elements("student").get(1);

        List list = student.elements();  //得到所有子结点[name,id,age]

        //创建新的结点
        Element addressnode = DocumentHelper.createElement("address");
        addressnode.setText("泉州");
        //插入新的结点,list变成了[name,address,id,age]，原来1后面的元素往后移动了一个位置
        list.add(1,addressnode);

        //重新写入
        //如果我们的xml编码格式为gb2312的话，那么字符集应该设置成format.setEncoding("gb2312");
        //也就是xml的字符集和匹配器设置的字符集得一致
        OutputFormat format = OutputFormat.createPrettyPrint();
        //这里指定的格式要和xml文档的编码格式一致，否则会出现乱码
        format.setEncoding("utf-8");

        //FileOutputStream的编码也是按照format设置的字符集进行编码
        XMLWriter writer = new XMLWriter(
                new FileOutputStream( "src/student.xml" ), format );

        //写入
        writer.write( document);
        //关闭
        writer.close();
    }

    //删除xml中的结点
    static void delete() throws DocumentException, IOException
    {
        SAXReader reader = new SAXReader();
        Document document = reader.read(new File("src/student.xml"));

        List studentlist = document.getRootElement().elements("student");
        Element student2 = (Element) studentlist.get(1);
        //取得所有属性
        List attributes = student2.elements();
        Element e = (Element) attributes.get(1);

        //实现删除,取得父亲，删除自己
        e.getParent().remove(e);

        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("utf-8");

        XMLWriter writer = new XMLWriter(
                new FileOutputStream( "src/student.xml" ), format );
        writer.write( document);
        writer.close();
    }


}
